
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">营业执照号：</label>
                                    <div class="layui-input-inline">
                                        <input name="customerLicense" class="layui-input" type="text" placeholder="请输入营业执照号" autocomplete="off"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">企业名称：</label>
                                    <div class="layui-input-inline">
                                        <input name="customerName" class="layui-input" type="text" placeholder="请输入企业名称" autocomplete="off"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit layui-form-keyDownSearch="true">查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                            <div class="query-rt"></div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="companyInfoBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
                        </div>
                        <div class="lay-btn-rt">
                            <button id="companyInfoBtnExport" class="layui-btn icon-btn color-green">导出</button>
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="warehouseCustomerTable" lay-filter="warehouseCustomerTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="companyInfoTableBar">
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="warehouseCustomerForm">
    <form lay-filter="warehouseCustomerForm" class="layui-form open-form">
        <input name="id" type="hidden"/>
        <div class="open-model">
            <div class="open-auto">
                <div class="layui-col-xs6">
                    <div class="layui-form-item">
                        <label class="layui-form-label"><span style="color: red">*</span>企业名称</label>
                        <div class="layui-input-block">
                            <input name="customerName" placeholder="请输入企业名称" type="text" class="layui-input" maxlength="200" lay-verify="required|isOnlyName" required autocomplete="off"/>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <div class="layui-form-item">
                        <label class="layui-form-label"><span style="color: red">*</span>营业执照号</label>
                        <div class="layui-input-block">
                            <input name="customerLicense" placeholder="请输入营业执照号" type="text" class="layui-input" maxlength="200" lay-verify="required" required autocomplete="off"/>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">省</label>
                        <div class="layui-input-block">
                            <input type="hidden" name="provinceName" />
                            <select name="province"  id="province" lay-filter="province" ></select>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">市</label>
                        <div class="layui-input-block">
                            <input type="hidden" name="cityName" />
                            <select name="city" id="city" lay-filter="city" ></select>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">区</label>
                        <div class="layui-input-block">
                            <input type="hidden" name="areaName" />
                            <select name="area" id="area" lay-filter="area" ></select>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">详细地址</label>
                        <div class="layui-input-block">
                            <input name="address" placeholder="请输入详细地址" type="text" maxlength="200" class="layui-input" />
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">客户来源</label>
                        <div class="layui-input-block">
                            <input name="remark" placeholder="客户来源" type="text" maxlength="200" class="layui-input" autocomplete="off"/>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item text-right">
                <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
                <button class="layui-btn" lay-filter="warehouseSubmit" lay-submit>保存</button>
            </div>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>

    var personGpsValue='';
    var getValue = function (type,lat,lng) {
        if(type===1){
            personGpsValue = lat+";"+lng;
            document.getElementById("coordinate").value =personGpsValue
        }
    };
    layui.use(['layer', 'form', 'table', 'admin', 'IconFonts', 'upload'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var IconFonts = layui.IconFonts;

        var clientIndustry =[];
        // 渲染表格
        var insTb = table.render({
            elem: '#warehouseCustomerTable',
            url: config.base + 'warehouse/warehouse/admin/customer/list',
            toolbar: true,
            defaultToolbar: ["filter"],
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'customerName', align:'center', title: '企业名称', width: 240}
                ,{field: 'customerLicense', align:'center', title: '营业执照号'}
                ,{field: 'provinceName',  align:'center', title: '省'}
                ,{field: 'cityName',  align:'center', title: '市'}
                ,{field: 'areaName',  align:'center', title: '区'}
                ,{field: 'address', align:'center', title: '客户地址'}
                ,{align: 'center', toolbar: '#companyInfoTableBar', title: '操作', width: 200, fixed: 'right'}
            ]],
            done: function (res, curr, count) {
                //移除按钮
                var menuButton = config.getMenuButton(location.hash);
                if(menuButton.indexOf("搜索") == -1){
                    $('.layui-form.toolbar').remove();
                }
                if(menuButton.indexOf("修改") == -1){
                    $("[lay-event='edit']").remove();
                }
                if(menuButton.indexOf("删除") == -1){
                    $("[lay-event='del']").remove();
                }
            }
        });

        // 工具条点击事件
        table.on('tool(warehouseCustomerTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(warehouseCustomerTable)', function(obj){
            table.reload('warehouseCustomerTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field,page: {curr: 1}},'data');
        });

        // 重置搜索
        form.on('submit(resets)', data => {
            insTb.reload({where: admin.resetSearch(data.field), page: {curr: 1}}, 'data');
        });

        // 添加按钮点击事件
        $('#companyInfoBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: ['1120px', '85%'],
                title: data ? '修改企业基本信息' : '添加企业基本信息',
                content: $('#warehouseCustomerForm').html(),
                success: function () {
                    form.val('warehouseCustomerForm', data);
                    // 表单提交事件
                    form.on('submit(warehouseSubmit)', function (d) {
                        layer.load(2);
                        admin.req(data ? 'warehouse/warehouse/admin/customer/update' : 'warehouse/warehouse/admin/customer/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('warehouseCustomerTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                    // 获取省区域信息
                    admin.req('enterprise/area/getProvinceList', {}, function(res) {
                        layer.closeAll('loading');
                        if (200 == res.code) {
                            $('#province').append(new Option("", ""));//往下拉菜单里添加元素
                            $.each(res.data, function(index, item) {
                                $('#province').append(new Option(item.name, item.id));//往下拉菜单里添加元素
                            })
                            if (data!=undefined&&data.province!=undefined){
                                // 获取市区域信息
                                var province = data.province;
                                admin.req('enterprise/area/getCityList?parentId=' + province,{}, function(res) {
                                    layer.closeAll('loading');
                                    if (200 == res.code) {
                                        $('#city').append(new Option("请选择市", ""));//往下拉菜单里添加元素
                                        $.each(res.data, function(index, item) {
                                            $('#city').append(new Option(item.name, item.id));//往下拉菜单里添加元素
                                        })
                                        form.val('warehouseCustomerForm', data);
                                        form.render(); //菜单渲染 把内容加载进去
                                    } else {
                                        layer.msg('获取(市)区域信息失败!', {
                                            icon : 2,
                                            time : 1000
                                        });
                                    }
                                }, 'POST');
                            }
                            if(data!=undefined&&data.city!=undefined){
                                // 获取区县区域信息
                                var city = data.city;
                                admin.req('enterprise/area/getAreaList?parentId=' + city,{}, function(res) {
                                    layer.closeAll('loading');
                                    if (200 == res.code) {
                                        $('#area').append(new Option("请选择区", ""));//往下拉菜单里添加元素
                                        $.each(res.data, function(index, item) {
                                            $('#area').append(new Option(item.name, item.id));//往下拉菜单里添加元素
                                        })
                                        form.val('warehouseCustomerForm', data);
                                        form.render(); //菜单渲染 把内容加载进去
                                    } else {
                                        layer.msg('获取(区县)区域信息失败!', {
                                            icon : 2,
                                            time : 1000
                                        });
                                    }
                                }, 'POST');
                            }
                            form.val('warehouseCustomerForm', data);
                            form.render(); //菜单渲染 把内容加载进去
                        } else {
                            layer.msg('获取(省)区域信息失败!', {
                                icon : 2,
                                time : 1000
                            });
                        }
                    }, 'POST', true);

                    form.on('select(province)', function(data) {
                        $("#city").empty();
                        $("#area").empty();
                        if(data.value){
                            // 获取市区域信息
                            admin.req('enterprise/area/getCityList?parentId=' + data.value,{}, function(data) {
                                layer.closeAll('loading');
                                if (200 == data.code) {
                                    $('#city').append(new Option("请选择市", ""));//往下拉菜单里添加元素
                                    $.each(data.data, function(index, item) {
                                        $('#city').append(new Option(item.name, item.id));//往下拉菜单里添加元素
                                    })
                                    form.render(); //菜单渲染 把内容加载进去
                                } else {
                                    layer.msg('获取(市)区域信息失败!', {
                                        icon : 2,
                                        time : 1000
                                    });
                                }
                            }, 'POST');
                        }else{
                            form.render(); //菜单渲染 把内容加载进去
                        }
                    });

                    form.on('select(city)', function(data) {
                        $("#area").empty();
                        if(data.value){
                            // 获取区县区域信息
                            admin.req('enterprise/area/getAreaList?parentId=' + data.value,{}, function(data) {
                                layer.closeAll('loading');
                                if (200 == data.code) {
                                    $('#area').append(new Option("请选择区", ""));//往下拉菜单里添加元素
                                    $.each(data.data, function(index, item) {
                                        $('#area').append(new Option(item.name, item.id));//往下拉菜单里添加元素
                                    })
                                    form.render(); //菜单渲染 把内容加载进去
                                } else {
                                    layer.msg('获取(区县)区域信息失败!', {
                                        icon : 2,
                                        time : 1000
                                    });
                                }
                            }, 'POST');
                        }else{
                            form.render(); //菜单渲染 把内容加载进去
                        }
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('warehouse/warehouse/admin/customer/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        $('#companyInfoBtnExport').click(function () {
            var checkRows = table.checkStatus('warehouseCustomerTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls', '企业基本信息');
            }
        });
        // 获取全部菜单
        // admin.req('user/admin/companyInfo/tree', {}, function (res) {
        //     layer.closeAll('loading');
        //     if (res.code == 200) {
        //         // 如果res.data为空对象，则将clientIndustry设置为空数组
        //         if (Object.keys(res.data).length === 0) {
        //             clientIndustry = [];
        //         } else {
        //             clientIndustry = res.data;
        //         }
        //     } else {
        //         layer.msg(res.msg, {icon: 2});
        //     }
        // }, 'get');

        //parentIdSelect.setValue([ menu.parentId ]);

    });
</script>